"use strict"

function smi(i32) {
    return i32 >>> 1 & 0x40000000 | i32 & 0xbfffffff;
}

function hashString(string) {
    // This is the hash from JVM
    // The hash code for a string is computed as
    // s[0] * 31 ^ (n - 1) + s[1] * 31 ^ (n - 2) + ... + s[n - 1],
    // where s[i] is the ith character of the string and n is the length of
    // the string. We "mod" the result to make it between 0 (inclusive) and 2^31
    // (exclusive) by dropping high bits.
    let hash = 0;
    for (let ii = 0; ii < string.length; ii++) {
        hash = 31 * hash + string.charCodeAt(ii) | 0;
    }
    return (2147483647 + smi(hash)).toString(32);
}

function analysisSvg(root) {
    $('xtb-svg').each((i, t) => {
        var $t = $(t)
        var fill = $(t).data('fill')
        var $elm = $t.parent()
        $.ajax({
            url: $t.data('src'),
            dataType: 'text',
            success: function (res) {
                $elm
                    .html(res)
                    .children('svg')
                    .attr('width', '100%')
                    .attr('height', '100%')
                    .children()
                    .css('fill', fill)
                    .end()
                    .get(0)
                    .setAttribute('preserveAspectRatio', 'none')
            },
        })
    })
}

function hashCard(store) {
    const inputs = MODE === 'card'
        ? ['pages', "title", "description", "secret", "groom_name", "bride_name", "groom_phone", "bride_phone", "address", "song_id", "user_cover", "wedding_time", "switch_mode", "loop", "autoplay", "visit_mode", 'delay', 'musicAutoPlay']
        : ['pages', "song_id"]
    let { card: { pages }, global: { map, cover }, music: { selected } } = store.getState();

    const data = inputs.map(t => {
        switch(t) {
            case 'pages':
                return JSON.stringify(pages)
            case 'address':
                return JSON.stringify(map)
            case 'song_id':
                return (selected ? selected.id || 0 : 0).toString()
            case 'user_cover':
                return cover
            case 'description':
                return document.querySelector(`textarea[name=${t}]`).value
            case 'loop':
            case 'autoplay':
            case 'musicAutoPlay':
                return document.querySelector(`input[name=${t}]`).checked ? 1 : 0
            case 'switch_mode':
            case 'delay':
            case 'visit_mode':
                return document.querySelector(`select[name=${t}]`).value
            default: 
                return document.querySelector(`input[name=${t}]`).value
        }
    }).join()

    return hashString(data);
}

function createElm (elm) {
    let $elm = document.createElement('div')
    
    $elm.innerHTML = elm.content

    for (let s in elm.css) {
        $elm.style[s] = elm.css[s]
    }

    return $elm
}

function convertTextElm (elm) {
    let set = (value, prop, target) => {target[prop] = value; return target}

    , getStyle = (prop, target) => target.style[prop] || (target.nodeName === 'FONT' && target[prop])

    , pick = ['color', 'lineHeight', 'textAlign', 'fontSize', 'backgroundColor']

    , style = pick.reduce((p, t) => t in elm.css ? set(elm.css[t], t, p) : p, {})

    , _pick = node => 
        pick.reduce
            ( (p, s) => getStyle(s, node) ? set(getStyle(s, node), s, p) : p
            , {}
            )
    //DA XIE DE NIU BI
    , pickStyle = (node) => 
        Array
            .from(node.childNodes)
            .reduce
                ( (p, t) => t.nodeType === 3 ? p : Object.assign(p, pickStyle(t))
                , _pick(node)
                )

    return Object.assign
        ( elm
        , 
            { css: Object.assign(elm.css, pickStyle(createElm(elm)))
            , content: ''
            }
        )
}

module.exports = {
    hashString,
    analysisSvg,
    hashCard,
    convertTextElm
}